package com.sgb.jicai.weight;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.qgb.zutils.ThreadUtils;
import com.sgb.jicai.R;
import com.sgb.jicai.entity.OfferGroupBuyingDetailsEntity;
import com.swgk.core.dialog.BasePopWindow;
import com.swgk.core.dialog.PopViewHolder;
import com.swgk.core.util.MToast;
import com.swgk.core.util.ResolutionUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.List;

public class PriceRolePopWindow extends BasePopWindow implements View.OnClickListener {

    private List<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> dataList;

    private XRecyclerView recyclerView;
    private ImageView ivAdd;
    private TextView tvConfirm;
    private View viewEmpty;
    private PriceRoleListAdapter adapter;
    private Context context;

    private ConfirmCallBack confirmCallBack;

    public PriceRolePopWindow(Context context) {
        super(context, DIR_DOWN_UP);
        this.context = context;
    }

    @Override
    protected int popLayout() {
        return R.layout.popwindow_price_role;
    }

    @Override
    protected void initView(PopViewHolder holder) {
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        setHeight(ResolutionUtil.getScreenHeight(mContext) * 2 / 3);

        recyclerView = holder.getPop().findViewById(R.id.recycler_view);
        ivAdd = holder.getPop().findViewById(R.id.iv_add);
        tvConfirm = holder.getPop().findViewById(R.id.tv_confirm);
        viewEmpty = holder.getPop().findViewById(R.id.view_empty);
        ivAdd.setOnClickListener(this::onClick);
        tvConfirm.setOnClickListener(this::onClick);
        viewEmpty.setOnClickListener(this::onClick);


        adapter = new PriceRoleListAdapter(mContext, dataList);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        DividerItemDecoration divider = new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(mContext, R.drawable.shape_list_divide_joinin_details_jc));
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.addItemDecoration(divider);
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setAdapter(adapter);
        recyclerView.setLoadingMoreEnabled(false);
        adapter.setDelOneDataCallBack(new PriceRoleListAdapter.DelOneDataCallBack() {
            @Override
            public void delOne(OfferGroupBuyingDetailsEntity.GroupBuyingDetails priceRule) {
                dataList.remove(priceRule);
                adapter.setDatas(dataList);
            }
        });
    }

    public void setDataList(List<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> dataList) {
        if (null == dataList || dataList.isEmpty()) {
            //没有初始报价信息，手动添加一条
            OfferGroupBuyingDetailsEntity.GroupBuyingDetails priceRule = new OfferGroupBuyingDetailsEntity.GroupBuyingDetails();
            priceRule.setSort("0");
            priceRule.setIntervalStartValue("");
            priceRule.setIntervalEndValue("");
            priceRule.setPrice("");
            dataList.add(priceRule);
        }
        this.dataList = dataList;
        adapter.setDatas(dataList);
        if (dataList.size() >= 5) {
            ivAdd.setBackgroundResource(R.drawable.add_gray);
        } else {
            ivAdd.setBackgroundResource(R.drawable.add_bright);
        }
    }

    //添加一条报价规则
    public void addOnePriceRole() {
        ivAdd.setFocusableInTouchMode(true);
        ivAdd.setFocusable(true);
        ivAdd.requestFocus();
        if (dataList.size() >= 5) {
            MToast.showToast(mContext, "最多只能添加五条报价信息");
            return;
        }
        if (null != dataList && dataList.size() > 0) {
            if (TextUtils.isEmpty(dataList.get(dataList.size() - 1).getIntervalStartValue())) {
                MToast.showToast(mContext, "请先填写最小值");
                return;
            }
            if (TextUtils.isEmpty(dataList.get(dataList.size() - 1).getIntervalEndValue())) {
                MToast.showToast(mContext, "请先填写最大值");
                return;
            }
            if (TextUtils.isEmpty(dataList.get(dataList.size() - 1).getPrice())) {
                MToast.showToast(mContext, "请先填写价格");
                return;
            }
            OfferGroupBuyingDetailsEntity.GroupBuyingDetails priceRule = new OfferGroupBuyingDetailsEntity.GroupBuyingDetails();
            priceRule.setSort(dataList.size() + "");
            //priceRule.setIntervalStartValue(String.valueOf(Integer.valueOf(dataList.get(dataList.size() - 1).getIntervalEndValue()) + 1));
            priceRule.setIntervalStartValue(String.valueOf(Double.valueOf(dataList.get(dataList.size() - 1).getIntervalEndValue()) + 1));
            priceRule.setIntervalEndValue("");
            priceRule.setPrice("");
            dataList.add(priceRule);
            adapter.setDatas(dataList);
            if (dataList.size() >= 5) {
                ivAdd.setBackgroundResource(R.drawable.add_gray);
            } else {
                ivAdd.setBackgroundResource(R.drawable.add_bright);
            }
        }
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_add) {
            addOnePriceRole();
        } else if (id == R.id.tv_confirm) {
            canConfirm();
        } else if (id == R.id.view_empty) {
            hideKeyboard(viewEmpty);
            dismiss();
        }
    }

    public void setConfirmCallBack(ConfirmCallBack confirmCallBack) {
        this.confirmCallBack = confirmCallBack;
    }

    public interface ConfirmCallBack {
        void getRolesData(List<OfferGroupBuyingDetailsEntity.GroupBuyingDetails> dataList);
    }

    public void canConfirm() {
        tvConfirm.setFocusableInTouchMode(true);
        tvConfirm.setFocusable(true);
        tvConfirm.requestFocus();
        //判断所有项填写完整
        for (int i = 0; i < dataList.size(); i++) {
            OfferGroupBuyingDetailsEntity.GroupBuyingDetails rule = dataList.get(i);
            if (TextUtils.isEmpty(rule.getIntervalStartValue())) {
                MToast.showToast(mContext, "第" + (i + 1) + "项未填写最小值");
                return;
            }
            if (i != dataList.size() - 1) {//最后一项的最大值可不填写
                if (TextUtils.isEmpty(rule.getIntervalEndValue())) {
                    MToast.showToast(mContext, "第" + (i + 1) + "项未填写最大值");
                    return;
                }
            }
            if (TextUtils.isEmpty(rule.getPrice())) {
                MToast.showToast(mContext, "第" + (i + 1) + "项未填写价格");
                return;
            }
        }
        if (null != confirmCallBack) {
            confirmCallBack.getRolesData(dataList);
        }
        hideKeyboard(tvConfirm);
        dismiss();
    }


    /**
     * 获取InputMethodManager，隐藏软键盘
     */
    //隐藏软键盘
    public void hideKeyboard(View view) {
        ThreadUtils.runOnSubThread(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
            }
        });
    }


}
